{
 "metadata": {
  "name": "",
  "signature": "sha256:61a4534a0a42ef4e1d260a141c9c91b8bbea92051a8994914043807162b8172b"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = np.genfromtxt('Advertising.csv', delimiter=\",\")\n",
      "x_data = data[1:,1:4]\n",
      "y_data = data[1:,4]\n",
      "print (len(x_data[0]))\n",
      "print (len(y_data))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n",
        "200\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def compute_loss(b, k, x_data, y_data):\n",
      "    loss = 0\n",
      "    for i in range(len(x_data)):\n",
      "        z = 0\n",
      "        for j in range(len(k)):\n",
      "            z += k[j]*x_data[i][j]\n",
      "        loss += (y_data[i] - (z+b))**2\n",
      "    return loss/float(len(x_data)) / 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def gradient_discent(x_data, y_data, lr, epoch, k, b):\n",
      "    batch = len(x_data)\n",
      "    for i in range(epoch):\n",
      "        dz = 0\n",
      "        dk = [0 for _ in range(len(k))]\n",
      "        db = 0\n",
      "        for j in range(batch):\n",
      "            z = 0\n",
      "            for n in range(len(k)):\n",
      "                z += k[n]*x_data[j][n]\n",
      "            dz = -(y_data[j] - (z+b))\n",
      "            for n in range(len(k)):\n",
      "                dk[n] += x_data[j][n] * dz / float(batch)\n",
      "            db += dz / float(batch)\n",
      "        for n in range(len(k)):\n",
      "            k[n] -= lr[n] * dk[n]\n",
      "        b -= lr[0] * db\n",
      "    return k, b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b = 0\n",
      "k = [0 for _ in range(len(x_data[0]))]\n",
      "epoch = 100\n",
      "lr = [0.000001 for _ in range(len(x_data[0]))]\n",
      "print (\"Start b = {0}, k = {1}, loss = {2}\".format(b, k, compute_loss(b, k, x_data, y_data)))\n",
      "print (\"Running.......\")\n",
      "k, b = gradient_discent(x_data, y_data, lr, epoch, k, b)\n",
      "print (\"After {3} epochs: b = {0}, k = {1}, loss = {2}\".format(b, k, compute_loss(b, k, x_data, y_data), epoch))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Start b = 0, k = [0, 0, 0], loss = 111.85812500000002\n",
        "Running.......\n",
        "After 100 epochs: b = 0.0005451538568089267, k = [0.075061049374910649, 0.016388151225817858, 0.017995224780363209], loss = 9.31831976939617"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = np.genfromtxt('Advertising.csv', delimiter=\",\")\n",
      "x_data = data[1:,2:4]\n",
      "y_data = data[1:,4]\n",
      "print (len(x_data[0]))\n",
      "print (len(y_data))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n",
        "200\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b = 0\n",
      "k = [0 for _ in range(len(x_data[0]))]\n",
      "epoch = 400\n",
      "lr = [0.001 for _ in range(len(x_data[0]))]\n",
      "print (\"Start b = {0}, k = {1}, loss = {2}\".format(b, k, compute_loss(b, k, x_data, y_data)))\n",
      "print (\"Running.......\")\n",
      "k, b = gradient_discent(x_data, y_data, lr, epoch, k, b)\n",
      "print (\"After {3} epochs: b = {0}, k = {1}, loss = {2}\".format(b, k, compute_loss(b, k, x_data, y_data), epoch))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Start b = 0, k = [0, 0], loss = 111.85812500000002\n",
        "Running.......\n",
        "After 400 epochs: b = 0.824601004204622, k = [0.36047299862109966, 0.093764757850761909], loss = 17.183348403625363"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ax = plt.figure().add_subplot(111, projection='3d')\n",
      "ax.scatter(x_data[:,0], x_data[:,1], y_data, c='r', marker='o', s=100)\n",
      "x0 = x_data[:,0]\n",
      "x1 = x_data[:,1]\n",
      "x0,x1 = np.meshgrid(x0,x1)\n",
      "z = k[0] * x0 + k[1] * x1 + b\n",
      "ax.plot_surface(x0,x1,z)\n",
      "ax.set_xlabel('x0')\n",
      "ax.set_ylabel('x1')\n",
      "ax.set_zlabel('z')\n",
      "\n",
      "plot.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "ValueError",
       "evalue": "Unknown projection '3d'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-27-7d7c7d87c82c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0max\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_subplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m111\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprojection\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'3d'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py\u001b[0m in \u001b[0;36madd_subplot\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1050\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1051\u001b[0m             projection_class, kwargs, key = process_projection_requirements(\n\u001b[0;32m-> 1052\u001b[0;31m                 self, *args, **kwargs)\n\u001b[0m\u001b[1;32m   1053\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1054\u001b[0m             \u001b[0;31m# try to find the axes with this key in the stack\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
        "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/matplotlib/projections/__init__.py\u001b[0m in \u001b[0;36mprocess_projection_requirements\u001b[0;34m(figure, *args, **kwargs)\u001b[0m\n\u001b[1;32m     89\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     90\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprojection\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstring_types\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mprojection\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 91\u001b[0;31m         \u001b[0mprojection_class\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_projection_class\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprojection\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     92\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprojection\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'_as_mpl_axes'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     93\u001b[0m         \u001b[0mprojection_class\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mextra_kwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprojection\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_as_mpl_axes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
        "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/matplotlib/projections/__init__.py\u001b[0m in \u001b[0;36mget_projection_class\u001b[0;34m(projection)\u001b[0m\n\u001b[1;32m     63\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mprojection_registry\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_projection_class\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprojection\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     64\u001b[0m     \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 65\u001b[0;31m         \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Unknown projection '%s'\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mprojection\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     66\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     67\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
        "\u001b[0;31mValueError\u001b[0m: Unknown projection '3d'"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "<matplotlib.figure.Figure at 0x7f4a59a40f60>"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}